package com.cuiyt.plugin;
import com.alibaba.druid.pool.DruidDataSource;
import com.cuiyt.Page;
import com.cuiyt.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.util.ArrayList;
import java.util.Properties;
/**
 * @author: cuiyutian1
 * @date: 2022/1/3 16:19
 * @description:
 */
public class PluginTest {

    @Test
    public void tetTx() throws Exception {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/ssm?characterEncoding=utf-8&serverTimezone=GMT&useSSL=false");
        dataSource.setUsername("root");
        dataSource.setPassword("cui0411..");

        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Interceptor[] plugins = new PageInterceptor[1];

        //配置分页插件
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties props = new Properties();
        props.setProperty("helperDialect", "mysql");
        props.setProperty("supportMethodsArguments", "true");
        props.setProperty("rowBoundsWithCount", "true");
        pageInterceptor.setProperties(props);
        ArrayList<PageInterceptor> pageInterceptors = new ArrayList<>();
        pageInterceptors.add(pageInterceptor);
        factory.setPlugins(pageInterceptors.toArray(plugins));
        factory.setConfigLocation(resolver.getResource("classpath:mybatis.xml"));
        factory.setMapperLocations(resolver.getResources("classpath:UserMapper.xml"));
        factory.setDataSource(dataSource);

        /**
         * 1 得到 sqlsessionfactory
         * 2 得到 sqlsession
         * 3 得到想要的mapper
         */
        SqlSessionFactory sessionFactory = factory.getObject();
        SqlSession session = sessionFactory.openSession();
        UserMapper mapper = session.getMapper(UserMapper.class);

        PageHelper.startPage(new Page().getPageNum(),new Page().getPageSize());
        mapper.page(null).forEach(System.out::println);
        // mapper.sltOnePlugin("")
    }
}
